package flt;

import java.io.ByteArrayInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.awt.*;

import java_cup.runtime.Symbol;

import javax.swing.*;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;
import javax.swing.tree.*;

public class HelloCUP extends JFrame {
	JTextArea area;
	JTextPane area1;
	public HelloCUP() {
		setSize(600,600);
		area= new JTextArea();
		area1 = new JTextPane();
		area.getDocument().addDocumentListener(new DocumentListener() {
			
			@Override
			public void removeUpdate(DocumentEvent e) {
				formatText();
				
			}
			
			@Override
			public void insertUpdate(DocumentEvent e) {
				formatText();
				
			}
			
			@Override
			public void changedUpdate(DocumentEvent e) {
				// TODO Auto-generated method stub
				
			}
		});
		setLayout(null);
		add(area);
		area.setBounds(0, 0, 300,300);
		area1.setBounds(0,300,300,600);
		add(area1);
		setVisible(true);
		setTitle("Cup editor");
	}
	public void formatText() {
		String inputFile = "input.txt";
		Symbol sym = null;
		String text= area.getText();
		InputStream is = new ByteArrayInputStream( text.getBytes( Charset.defaultCharset() ) );
		
		try {
			CalcLexer lexer = new CalcLexer(is);
			CalcParser parser = new CalcParser(lexer);
			
			sym = parser.parse();
			area1.setText("<html>"+sym.value.toString()+"</html>");
			System.out.println("\n\n\n<html>"+sym.value.toString()+"</html>\n\n\n\n");
			
			
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
		
		//ArrayList<Integer> a = new ArrayList<Integer>();
		//a.remove(arg0);
		System.out.println(" DONE ");
		if (sym != null)
		{
			System.out.println("Result = " + sym.value);
		}
	}
	public static void main(String[] args) {
		
		HelloCUP cup = new HelloCUP();
		
		
		//ArrayList<Integer> a = new ArrayList<Integer>();
		//a.remove(arg0);
		//new Computer((Node)sym.value);
	}
		
}

/*
//----------------------------------------------------
//The following code was generated by CUP v0.11a beta 20060608
//Fri Mar 30 20:51:13 EEST 2012
//----------------------------------------------------

package flt;

*//** CUP generated interface containing symbol constants. *//*
public interface sym {
 terminals 
public static final int SUM = 7;
public static final int LACOL = 8;
public static final int NUMBER = 10;
public static final int EOF = 0;
public static final int TAIL = 5;
public static final int HEAD = 4;
public static final int RACOL = 9;
public static final int error = 1;
public static final int RPAREN = 3;
public static final int LPAREN = 2;
public static final int CONS = 6;
}

*/
